
export const getQueryString = (name) => {
  try {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    let r = window.location.search.substr(1).match(reg)
    if (r != null) {
      return decodeURIComponent(r[2])
    }
    r = window.location.hash.substr(window.location.hash.indexOf('?') + 1).match(reg)
    if (r != null) {
      return decodeURIComponent(r[2])
    }
    return null
  } catch (e) {
    return null
  }
}
function getMinusTime (time) {
  let timeObj = {}
  timeObj.years = Math.floor(time / (365 * 24 * 60 * 60 * 1000))

  let leave1 = time % (365 * 24 * 60 * 60 * 1000)
  timeObj.months = Math.floor(leave1 / (30 * 24 * 60 * 60 * 1000))

  let leave2 = leave1 % (30 * 24 * 60 * 60 * 1000)
  timeObj.days = Math.floor(leave2 / (24 * 60 * 60 * 1000))

  let leave3 = leave2 % (24 * 60 * 60 * 1000)
  timeObj.hours = Math.floor(leave3 / (60 * 60 * 1000))

  let leave4 = leave3 % (3600 * 1000)
  timeObj.minutes = Math.floor(leave4 / (60 * 1000))

  let leave5 = leave4 % (60 * 1000)
  timeObj.seconds = Math.round(leave5 / 1000)
  return timeObj
}
// 将时间转化为多少天前
export const toHistoryDate = (datetime) => {
  if (!datetime) {
    return ''
  }
  let now = new Date()
  now = now.getTime()
  datetime = datetime.replace(/\.\d+/, '').replace(/-/g, '/')
  let historyTime = new Date(datetime).getTime()
  let minus = now - historyTime
  let o = getMinusTime(minus)
  let result
  if (o.years >= 10) {
    result = '10年前'
  } else if (o.years >= 5) {
    result = '5年前'
  } else if (o.years >= 3) {
    result = '3年前'
  } else if (o.years >= 1) {
    result = '1年前'
  } else if (o.months >= 6) {
    result = '半年前'
  } else if (o.months >= 3) {
    result = '3个月前'
  } else if (o.month >= 1) {
    result = '1个月前'
  } else if (o.days >= 15) {
    result = '半个月前'
  } else if (o.days >= 10) {
    result = '10天前'
  } else if (o.days >= 5) {
    result = '5天前'
  } else if (o.days >= 3) {
    result = '3天前'
  } else if (o.days > 2) {
    result = '前天'
  } else if (o.days > 1) {
    result = '昨天'
  } else if (o.hours >= 1) {
    result = o.hours + '小时前'
  } else if (o.minutes >= 1) {
    result = o.minutes + '分钟前'
  } else {
    result = '刚刚'
  }
  return result
}

// 处理线索状态
export const parseStatus = (index) => {
  let status = {
    '-2': '被动收回',
    '-1': '主动退回',
    '0': '待分配',
    '1': '已分配',
    '2': '跟进中',
    '3': '已转换'
  }
  return status[index]
}
// 处理联系人类型
export const parseRole = (index) => {
  let status = {
    '0': '发起人',
    '1': '使用者',
    '2': '影响者',
    '3': '守门人',
    '4': '购买者',
    '5': '决策者',
    '6': '相关人',
    '7': '参与人'
  }
  return status[index]
}
// 处理商机的销售阶段
export const parseSalePeriod = (index) => {
  let status = {
    '-3': '回收中断',
    '-2': '放弃',
    '1': '赢单',
    '-1': '输单',
    '2': '需求沟通',
    '3': '方案报价',
    '4': '商务流程',
    '5': '销售总结'
  }
  return status[index]
}
// 财务状态：预测
export const parseForecast = (index) => {
  return ['暂无预测', '已有预测'][index]
}
// 财务状态：开票
export const parseBill = (index) => {
  return [' 暂未开票', '已经开票'][index]
}
// 财务状态：回款
export const parsePayPeriod = (index) => {
  let status = {
    '0': '暂无回款',
    '1': '回款中',
    '2': '回款完成'
  }
  return status[index]
}
// 寻求支持的状态
export const parseSupportStatus = (index) => {
  let status = {
    '0': '运行中',
    '1': '已完毕',
    '2': '已驳回'
  }
  return status[index]
}
// 成交预测的把握程度
export const parseGraspDegree = (index) => {
  let status = {
    '1': '承诺',
    '2': '努力',
    '3': '冲刺'
  }
  return status[index]
}
// 开票的状态
export const parseBillStatus = (index) => {
  let status = {
    '0': '申请中',
    '1': '已开具',
    '2': '已邮递',
    '-1': '未通过',
    '-2': '撤回'
  }
  return status[index]
}
// 结项的状态
export const parseEndStatus = (index) => {
  let status = {
    '0': '运行中',
    '1': '已完毕',
    '2': '已驳回'
  }
  return status[index]
}
// 加上百分比符号
export const parseRate = (rate) => {
  return rate + '%'
}

export const isNullOrEmpty = (s) => {
  if (s === null || s === '') {
    return true
  } else {
    return false
  }
}
// 将时间转化为指定格式
export const dateToString = (text, format) => {
  if (isNullOrEmpty(text)) {
    return ''
  }
  text = text.replace(/\.\d+/, '').replace(/-/g, '/')

  let date = new Date(text)
  if (isNullOrEmpty(date) || date === 'Invalid Date') {
    return ''
  }
  let dateStr = format
  dateStr = dateStr
    .replace('yyyy', date.getFullYear())
    .replace('MM', (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1))
    .replace('dd', (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()))
    .replace('HH', (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()))
    .replace('mm', (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()))
    .replace('ss', (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()))
  dateStr = dateStr
    .replace('M', date.getMonth() + 1)
    .replace('d', date.getDate())
    .replace('H', date.getHours())
    .replace('m', date.getMinutes())
    .replace('s', date.getSeconds())
  return dateStr
}
// 处理头像
export const parseAvatar = (imageUrl) => {
  if (isNullOrEmpty(imageUrl)) {
    imageUrl = require('../assets/images/default_avatar.jpg')
  }
  let imgeMogr = ''
  let width = 100
  let height = 100
  if (imageUrl.indexOf('http://wx.qlogo.cn') === -1 && width > 0 && height > 0) {
    // 设置分辨率
    imgeMogr = (imageUrl.indexOf('@') > 0 ? '|' : '@')
    imgeMogr += 'w_' + width + ',h_' + height
  }
  if (imageUrl.indexOf('http://') === 0) {
    return imageUrl + imgeMogr
  } else {
    return require('../assets/images/default_avatar.jpg')
  }
}
// 金钱三位一逗号
export const numFormat = (value) => {
  if (!value) return '0.00'
  let intPart = Number(value).toFixed(0) // 获取整数部分
  let intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断

  let floatPart = '.00' // 预定义小数部分
  let value2Array = value.split('.')

  // =2表示数据有小数位
  if (value2Array.length === 2) {
    floatPart = value2Array[1].toString() // 拿到小数部分
    if (floatPart.length === 1) { // 补0,实际上用不着
      return intPartFormat + '.' + floatPart + '0'
    } else {
      return intPartFormat + '.' + floatPart
    }
  } else {
    return intPartFormat + floatPart
  }
}
